<?php
class Model_ServerRPC
{
	/*
	 *callback from service site to check the skey when access service site from center account
	 */

	public function getClientByKeyID($input_arr)
	{
		$client_id = (isset($input_arr['client_id']))?$input_arr['client_id']:'';
		$security_key = (isset($input_arr['security_key']))?$input_arr['security_key']:'';

		// code here
		$model = new Model_Service();
		$info = $model->getClientServiceInfoByKey($client_id, $security_key);
		if (!$info) throw new Exception('Wrong key');

		error_log("client_id = ".$client_id);
		error_log("security_key = ".$security_key);
		//Zend_Debug::dump($info);exit;
		$dpt_id = $info['dpt_id'];
		$dpt_account_login = $info['dpt_account_login'];
		$dpt_account_id = $info['dpt_account_id'];

		error_log("dpt_id = ".$dpt_id);
		error_log("dpt_account_login = ".$dpt_account_login);

		$password = ""; // only check status
		$service = Model_ServiceFactory::Create($dpt_id);
		$rs = $service->loginCheck($dpt_id, $dpt_account_login, $password);
		//Zend_Debug::dump($rs);
		error_log("status = ".$rs['status']);
		error_log("password = ".$password);
		if ($rs['status'])
		{
			$service->updateKey($dpt_id, $dpt_account_id, $client_id, $skey = "");

			$return_arr['client_id'] = $client_id;
			$return_arr['dpt_id'] = $dpt_id;
			$return_arr['dpt_account_id'] = $dpt_account_id;
			$return_arr['dpt_account_login'] = $dpt_account_login;
			$return_arr['login_area'] = $rs['login_area'];
			if (isset($rs['level_id'])) $return_arr['level_id'] = $rs['level_id'];
			if (isset($rs['password'])) $return_arr['password'] = $rs['password'];
			if (isset($rs['user_name'])) $return_arr['user_name'] = $rs['user_name'];
		}
		else throw new Exception($rs['message']);

		//for testing
		//$return_arr['client_id'] = 'f8b237aa0d7fd5e9d1f041f70a7647a2';
		//$return_arr['dpt_id'] = '1500';
		//$return_arr['dpt_account_id'] = '6';
		//$return_arr['dpt_account_login'] = 'hong';
		//$return_arr['login_area'] = 'admin';

		return $return_arr;
	}

	/*
	 *call from service site to get the skey and  login to center account from service site
	 */
	public function getKeyIDByAccount($input_arr)
	{
		$skey = md5(time().rand());
		
		$config = Zend_Registry::get('config');
		$direct_login_url = $config->remote->direct->login;
		
		$dpt_id = (isset($input_arr['dpt_id']))?$input_arr['dpt_id']:'';
		$dpt_account_id = (isset($input_arr['dpt_account_id']))?$input_arr['dpt_account_id']:'';
		$dpt_account_login = (isset($input_arr['dpt_account_login']))?$input_arr['dpt_account_login']:'';

		// code here
		$model_service = new Model_Service();
		$client_id = $model_service->getClientServiceInfo($dpt_id, $dpt_account_login);
		if ($client_id === -1)
		{
			$message = "Not subscribed yet.";
		} 
		elseif ($client_id === -2)
		{
			// multi subscribed
			$message = "Please use the YesUp account to login.";
		}  
		else
		{ 
			$model_service->updateKey($dpt_id, $dpt_account_id, $client_id, $skey);			
			$message = "OK";
		}
		
		$return_arr['client_id'] = $client_id;
		$return_arr['message'] = $message;
		$return_arr['security_key'] = $skey;
		$return_arr['direct_login'] = $direct_login_url;

		//for testing
		//$return_arr['client_id'] = 'f8b237aa0d7fd5e9d1f041f70a7647a2';
		//$return_arr['skey'] = '1234567890';

		return $return_arr;
	}
	
	
	/*
	 *call from service site to get the Yesup client_id
	 */
	public function getYesupIDByAccount($input_arr)
	{
		$dpt_id = (isset($input_arr['dpt_id']))?$input_arr['dpt_id']:'';
		$dpt_account_id = (isset($input_arr['dpt_account_id']))?$input_arr['dpt_account_id']:'';
		$email = (isset($input_arr['email']))?$input_arr['email']:'';
		$login_area = (isset($input_arr['login_area']))?$input_arr['login_area']:'member';
		//$password = (isset($input_arr['password']))?$input_arr['password']:'';
		if (!$email || !$dpt_id || !$dpt_account_id)
		{
			$client_id = "";
			$message = "missing fileds.";
			$url = "";			
			//error_log("client_id= , --".$message);
		}
		else
		{
			$config = Zend_Registry::get('config');
			$yesup_url = $config->yesup->account->url;

			$model_client = Model_UserFactory::create('client');
			$client_info = $model_client->getClientByEmail($email);

			if ($client_info) // exist user
			{
				$client_id =  $client_info['client_id'];				
				$linked_service = $model_client->getClientService($client_id, $dpt_id);
			
				
				$dpt_id_stored =  $dpt_id;
				$dpt_account_id_stored =  $dpt_account_id;
								
				$password = "";
				$message = 'OK';
				$url = $yesup_url."/client";   // login url;
				//error_log("client_id = $client_id, -- ".$message);
			}
			else // create one
			{
				$linked_service = ""; // need to link
				
				$dpt_id_stored =  $dpt_id;
				$dpt_account_id_stored =  $dpt_account_id;
				
				$id_type='yesup';
				$password = rand(111111, 999999);   // 
				$client_id = $model_client->remoteSignupClient($email, $password, $id_type);

				$url = $yesup_url."/client";   // login url;
				$message = 'OK';				
				//error_log("client_id = $client_id, -- ".$message.' password = $password');
			}
			
			$dpt_login_arr['login_area'] = $login_area;
			$dpt_login_arr['dpt_account_login'] = $email;
			$dpt_login_arr['password'] = $password;	
			$dpt_login_arr['admin_user_id'] = 0;
			$dpt_login_arr['admin_user_id'] = '';
							
			if (empty($linked_service)) // no link
					$rs = $model_client->linkClientService($client_id, $dpt_id, $dpt_account_id, $dpt_login_arr);
			else 
					$rs = $model_client->updateClientService($client_id, $dpt_id, $dpt_account_id, $dpt_login_arr);
		}
		
		$return_arr['client_id'] = $client_id;
		$return_arr['password'] = $password;
		$return_arr['url'] = $url;
		$return_arr['message'] = $message;

		return $return_arr;
	}
	
	
	/*
	 *call from service site to get user subscribed service
	 */
	public function getServiceById($input_arr)
	{
		$client_id = (isset($input_arr['client_id']))?$input_arr['client_id']:'';
		//$security_key = (isset($input_arr['security_key']))?$input_arr['security_key']:'';
		
		// code here
		//$model = new Model_Service();
		//$info = $model->getClientServiceInfoByKey($client_id, $security_key);
		//if (!$info) throw new Exception('Wrong key');

		error_log("client_id = ".$client_id);
	
		$user_role = 'client';		
		$model_client = Model_UserFactory::create($user_role);
		
		$return_arr = $model_client->getClientService($client_id);
		
		return $return_arr;
	}
	
	
	/*
	 *call from service site to get user subscribed service
	 */
	public function getClientById($input_arr)
	{
		$client_id = (isset($input_arr['client_id']))?$input_arr['client_id']:'';
		$user_role = 'client';		
		$model_client = Model_UserFactory::create($user_role);
		
		$return_arr = $model_client->getProfile($client_id);
		if (empty($return_arr)){
			$data = $model_client->getClient($client_id,$allow_pending='yes');
			$return_arr =array('email'=>$data['email']);
		}
		
		return $return_arr;
	}
	

	/*
	 *call from service site to get network authority check
	 */
	public function getNetworkClientByKeyID($input_arr)
	{
		$client_id = (isset($input_arr['client_id']))?$input_arr['client_id']:'';
		$security_key = (isset($input_arr['security_key']))?$input_arr['security_key']:'';

		// code here
		$user_role = 'client';		
		$model_client = Model_UserFactory::create($user_role);
		$info = $model_client->getNetworkClientByKey($client_id, $security_key);
		if (!$info) throw new Exception('Wrong key');
		else 
		{
			//$model_client->updateClientKey($info['client_id'], "");
			
			$skey = $info['partner_key']; 
			$pieces = explode("_",$skey); 
			
			
			$return_arr['client_id'] = $info['client_id'];
			$return_arr['dpt_id'] = $pieces['1'];
			$return_arr['dpt_account_id'] = '';
			$return_arr['dpt_account_login'] = '';
			$return_arr['login_area'] = 'partner';
		}
		
		return $return_arr;

	}
	
/*
	 *to check the linked dpt_account_id in central account sys
	 */
	public function checkLinked($input_arr)
	{
		$dpt_id = (isset($input_arr['dpt_id']))?$input_arr['dpt_id']:'';
		$dpt_account_id = (isset($input_arr['dpt_account_id']))?$input_arr['dpt_account_id']:'';
		$email = (isset($input_arr['email']))?$input_arr['email']:'';
		
		$config = Zend_Registry::get('config');
		$yesup_url = $config->yesup->account->url;
			
		$linked_service1 = array();
		if (!empty($email) && $email != 'n/a')
		{
			//error_log($email);
			$model_client = Model_UserFactory::create('client');
			$client_info = $model_client->getClientByEmail($email);
			
			if ($client_info)
			{
				$client_id =  $client_info['client_id'];				
				$linked_service1 = $model_client->getClientService($client_id);
			}			
		}
		
		$linked_service2 = array();
		if (!empty($dpt_id) && !empty($dpt_account_id))
		{
			//error_log($dpt_id."-".$dpt_account_id);
			$model_service = new Model_Service();
			$linked_service2 = $model_service->getClientServiceById($dpt_id, $dpt_account_id);
		}

		$return_arr = array_merge($linked_service1,$linked_service2);

		return $return_arr;
	}
	
}